package com.spring.draw.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.draw.api.web.dao.DrawApi2Mapper;
import com.draw.api.web.model.DrawJoiner;
import com.spring.draw.web.service.DrawApi2Service;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.util.UDate;
import com.spring.security.core.util.UpdateMap;
import com.draw.api.web.dao.Common2Mapper;
import com.spring.security.web.model.BaseResult;

@Service
public class DrawApi2ServiceImpl implements DrawApi2Service{
	
	@Resource private DrawApi2Mapper drawApi2Mapper;
	@Resource private Common2Mapper common2Mapper;

	@Override
	public List<DrawJoiner> drawJoiner(Map<String,Object> parm) {
		return drawApi2Mapper.drawJoiner(parm);
	}

	@Override
	public List<DrawJoiner> drawWinner(Map<String,Object> parm) {
		return drawApi2Mapper.drawWinner(parm);
	}
	
	@Override
	public BaseResult drawByWeb(Map<String, Object> parm) {
		List<Map<String, Object>> dnlist = drawApi2Mapper.drawAwardsNum(parm);
		if(dnlist!=null&&dnlist.size()>0){
			//奖项设置的中奖上限
			Integer sxnum = Integer.parseInt(ApplicationUtils.Object2String(dnlist.get(0).get("winningNumber")));
			//已中奖人
			List<DrawJoiner> haswin = drawApi2Mapper.drawWinner(parm);
			if(haswin.size()>=sxnum){
				BaseResult ret = new BaseResult("0","该奖项已达抽奖人数上限",null);
				return ret;
			}
		}
		
		Map<String,Object> parms = Maps.newHashMap();
		parms.put("eventId", ApplicationUtils.Object2String(parm.get("eventId")));
		parms.put("checkwxOpenId", ApplicationUtils.Object2String(parm.get("wxOpenId")));
		List<DrawJoiner> haswin = drawApi2Mapper.drawWinner(parms);
		if(haswin.size()>0){
			DrawJoiner hasone = haswin.get(0);
			BaseResult ret = new BaseResult("0","用户"+hasone.getWxNickName()+"已中"+hasone.getDrawAwardsName(),null);
			return ret;
		}
		
		DrawJoiner retd = new DrawJoiner();
		retd.setDrawEventId(ApplicationUtils.Object2String(parm.get("eventId")));
		retd.setDrawAwardsId(ApplicationUtils.Object2String(parm.get("awardId")));
		retd.setWxOpenId(ApplicationUtils.Object2String(parm.get("wxOpenId")));
		retd = updateWinFlag(retd);
		if(retd==null){
			BaseResult ret = new BaseResult("0","未获取到候选人名单",retd);
			return ret;
		}else{
			BaseResult ret = new BaseResult("1","操作成功",retd);
			return ret;
		}
	}

	@Override
	public BaseResult draw(Map<String,Object> parm) {
		List<Map<String, Object>> dnlist = drawApi2Mapper.drawAwardsNum(parm);
		if(dnlist!=null&&dnlist.size()>0){
			//奖项设置的中奖上限
			Integer sxnum = Integer.parseInt(ApplicationUtils.Object2String(dnlist.get(0).get("winningNumber")));
			//已中奖人
			List<DrawJoiner> haswin = drawApi2Mapper.drawWinner(parm);
			if(haswin.size()>=sxnum){
				BaseResult ret = new BaseResult("0","该奖项已达抽奖人数上限",null);
				return ret;
			}
		}
		//获取候选人
		List<DrawJoiner> jlist = drawApi2Mapper.drawJoiner(parm);
		DrawJoiner retD = null;
		List<DrawJoiner> hlist = Lists.newArrayList();
		//作弊
		for(DrawJoiner d:jlist){
			if(ApplicationUtils.Object2String(parm.get("awardId")).equalsIgnoreCase(d.getDrawAwardsId())&&
					"1".equalsIgnoreCase(d.getIsPreviewWon())){
				retD = updateWinFlag(d);
				if(retD==null){
					BaseResult ret = new BaseResult("0","未获取到候选人名单",retD);
					return ret;
				}else{
					BaseResult ret = new BaseResult("1","操作成功",retD);
					return ret;
				}
			}
			
			//如果是内定过奖人，则从候选人中剔除
			if("0".equalsIgnoreCase(d.getIsPreviewWon())){
				hlist.add(d);
			}
		}
		
		
		
		if(hlist!=null&&hlist.size()>0){
			int size = hlist.size();
			int num = (int)(1+Math.random()*(size-1+1));
			retD = hlist.get(num-1);
			retD.setDrawAwardsId(ApplicationUtils.Object2String(parm.get("awardId")));
			retD = updateWinFlag(retD);
		}
		if(retD==null){
			BaseResult ret = new BaseResult("0","未获取到候选人名单",retD);
			return ret;
		}else{
			BaseResult ret = new BaseResult("1","操作成功",retD);
			return ret;
		}
	}
	
	private DrawJoiner updateWinFlag(DrawJoiner d){
		if(StringUtils.isNotBlank(d.getDrawEventId())
				&&StringUtils.isNotBlank(d.getDrawAwardsId())
				&&StringUtils.isNotBlank(d.getWxOpenId())){
			UpdateMap um = new UpdateMap("draw_joiner");
			um.addField("isWon", "1");
			um.addField("ChangedTime", UDate.getDateTime());
			um.addField("draw_awardsID", d.getDrawAwardsId());
//			um.addWhere("draw_eventID", d.getDrawEventId());
//			um.addWhere("wxOpenID", d.getWxOpenId());
			um.addWhere("id", d.getId());
			//抽中
			int i = common2Mapper.update(um);
			if(i==0){
				return null;
			}
			d.setIsWon("1");
			//重新查询
			Map<String,Object> parm = Maps.newHashMap();
			parm.put("eventId",d.getDrawEventId());
			parm.put("checkwxOpenId",d.getWxOpenId());
//			parm.put("id",d.getId());
			List<DrawJoiner> retd = drawApi2Mapper.drawWinner(parm);
			if(retd!=null&&retd.size()>0){
				return retd.get(0);
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	@Override
	public Integer unDraw(String eventId, String awardId, String wxOpenId) {
		Map<String,Object> parm = Maps.newHashMap();
		parm.put("draw_eventID", eventId);
		parm.put("draw_awardsID", awardId);
		parm.put("wxOpenID", wxOpenId);
//		UpdateMap um = new UpdateMap("draw_joiner");
//		um.addField("isWon", "0");
//		//um.addField("draw_awardsID", "");
//		um.addWhere("draw_eventID", eventId);
//		um.addWhere("draw_awardsID", awardId);
//		um.addWhere("wxOpenID", wxOpenId);
//		common2Mapper.update(um);
		int i = drawApi2Mapper.removeDraw(parm);
		return i;
	}
	
	@Override
	public Integer unDraw2(String id) {
		Map<String,Object> parm = Maps.newHashMap();
		parm.put("id", id);
		int i = drawApi2Mapper.removeDraw2(parm);
		return i;
	}

	@Override
	public List<Map<String, Object>> drawEvent(String eventId) {
		Map<String,Object> parm = Maps.newHashMap();
		parm.put("eventId", eventId);
		return drawApi2Mapper.drawEvent(parm);
	}

	@Override
	public List<Map<String, Object>> drawAwards(Map<String, Object> parm) {
		return drawApi2Mapper.drawAwards(parm);
	}
	
	
	
	@Override
	public Map<String, Object> checkDrawID(String id) {
		Map<String, Object> m=new HashMap<String, Object>();
		
		Map<String, Object> parm=new HashMap<String, Object>();
		parm.put("id", id);
		
		String status=drawApi2Mapper.checkDrawID(parm);
		if(status==null){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "请输入有效id");
			return m;
		}else if(status.equals(0)){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "该场次尚未开放");
			return m;
		}
		
		return null;
	}

}
